package com.dlkj.threadPool.controller;

import com.alibaba.fastjson.JSONObject;
import com.dlkj.threadPool.core.constants.WeChatConstant;
import com.dlkj.threadPool.core.response.CallResult;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author : lei
 * @Company ：DiLang Technology
 * @Date : 2019/1/18 15:47
 * @Description :
 **/
@RestController
@Validated
public class WeChatController {

    @Resource
    private WeChatConstant weChatConstant;

    @GetMapping("/auth")
    public CallResult auth(@NotEmpty(message = "用户登录凭证不能为空") String code){
        // 根据小程序的用户登录凭证及appId、appSecret调用微信端code2Session接口
        RestTemplate template = new RestTemplate();
        String url = weChatConstant.getAppUrl() + "?appid=" + weChatConstant.getAppId()
                + "&secret=" + weChatConstant.getAppSecret()
                + "&js_code=" + code + "&grant_type="+weChatConstant.getGrantType();
        Map<String, String> params = new LinkedHashMap<>();
        params.put("appid",weChatConstant.getAppId());
        params.put("secret",weChatConstant.getAppSecret());
        params.put("js_code",code);
        params.put("grant_type",weChatConstant.getGrantType());
        String result = template.getForObject(url, String.class,params);

        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getString("errCode") == null) {
            return CallResult.callSuccess(jsonObject);
            // 存入 session 或者 存入 redis ......,返回 key 到小程序
        }
        return CallResult.callFail(result);
    }


    @GetMapping("/qrCode")
    public CallResult qrCode(){
        String access_token = "20_YY6QZD1l0_xKaEhXhm6tSMLDaYRxPhFu4LI1GwlzRTclNvoXdiOPyQzHksavv5RpiEubQqtypCw24-L1MP80IoY4jdv_oWmLfX64ph7K8qKOIVYpJLuydryxzinPXsCF8HWAnBEEstimCZ67PENiAGAAPY";
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("scene","a=1");
        RestTemplate template = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(params,headers);
        String result = template.postForObject(url,httpEntity ,String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("errcode") == 0) {
            return CallResult.callSuccess(jsonObject.getString("errmsg"));
        }
        return CallResult.callFail(jsonObject.getString("errmsg"));
    }
}
